﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.IO;
using DataAccessLayer.Adapter;
using BussinessLayer;
namespace QLTSLienThong.Form_he_thong
{
    public partial class frmMain : Office2007RibbonForm
    {
        public frmMain()
        {
            InitializeComponent();
        }
        public static string _Username,_Quyen;
        FileDialog flDialog;
        string _Path;
        BackupAndRestoreDataBase backupAndRestore = new BackupAndRestoreDataBase();
        private void buttonItem13_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Windows7Blue;
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Office2010Silver;
        }

        private void buttonItem4_Click(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Office2010Black;
        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Office2007VistaGlass;
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Office2013;
        }
        private void addNewTab(string strTabName, UserControl ucContent)
        {
            //-----------If exist tabpage then exit---------------
            foreach (TabItem tabPage in tabControl1.Tabs)
                if (tabPage.Text == strTabName)
                {
                    tabControl1.SelectedTab = tabPage;
                    return;
                }
            //-------------------------Clear Tab Before---------------
            //if (tabControl1.Tabs.Count > 1)
            //    tabControl1.Tabs.RemoveAt(1);

            TabControlPanel newTabPanel = new DevComponents.DotNetBar.TabControlPanel();
            TabItem newTabPage = new TabItem(this.components);
            newTabPage.ImageIndex = 0;
            //newTabPage.MouseDown += new System.Windows.Forms.MouseEventHandler(tabItem_MouseDown);
            newTabPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            newTabPanel.Location = new System.Drawing.Point(0, 26);
            newTabPanel.Name = "panel" + strTabName;
            newTabPanel.Padding = new System.Windows.Forms.Padding(1);
            newTabPanel.Size = new System.Drawing.Size(1230, 384);
            newTabPanel.Style.BackColor1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(142)))), ((int)(((byte)(179)))), ((int)(((byte)(231)))));
            newTabPanel.Style.BackColor2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(223)))), ((int)(((byte)(237)))), ((int)(((byte)(254)))));
            newTabPanel.Style.Border = DevComponents.DotNetBar.eBorderType.SingleLine;
            newTabPanel.Style.BorderColor.Color = System.Drawing.Color.FromArgb(((int)(((byte)(59)))), ((int)(((byte)(97)))), ((int)(((byte)(156)))));
            newTabPanel.Style.BorderSide = ((DevComponents.DotNetBar.eBorderSide)(((DevComponents.DotNetBar.eBorderSide.Left | DevComponents.DotNetBar.eBorderSide.Right)
                        | DevComponents.DotNetBar.eBorderSide.Bottom)));
            newTabPanel.Style.GradientAngle = 90;
            newTabPanel.TabIndex = 2;
            newTabPanel.TabItem = newTabPage;
            //-------------New  tab page---------------------
            Random ran = new Random();
            newTabPage.Name = strTabName + ran.Next(100000) + ran.Next(22342);
            newTabPage.AttachedControl = newTabPanel;
            newTabPage.Text = strTabName;
            ucContent.Dock = DockStyle.Fill;
            newTabPanel.Controls.Add(ucContent);
            //------------add Tab page to Tab control-------------
            tabControl1.Controls.Add(newTabPanel);
            tabControl1.Tabs.Add(newTabPage);
            tabControl1.SelectedTab = newTabPage;
        }

        private void buttonItem17_Click(object sender, EventArgs e)
        {
            ucCapNhatTinhThanh ucTinhThanh = new ucCapNhatTinhThanh();
            addNewTab("Tỉnh - Thành",ucTinhThanh);
        }

        private void tabControl1_TabItemClose(object sender, TabStripActionEventArgs e)
        {
            TabItem tabs = tabControl1.SelectedTab;
            if (MessageBox.Show("Bạn muốn đóng: \"" + tabs.Text + "\"?", "Thông báo!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                tabControl1.Tabs.Remove(tabs);
            }
        }
        
        private void buttonItem10_Click(object sender, EventArgs e)
        {
            try
            {
                flDialog = new SaveFileDialog();
                flDialog.Filter = "File type (*.bak)|*.bak|All file (*.*)|*.*";
                //flDialog.DefaultExt = ".bak";
                
                flDialog.ShowDialog();
                _Path = flDialog.FileName;
                if (_Path != "")
                {
                    backupAndRestore.BackupDatabase(_Path);
                    MessageBox.Show("Backup thành công: ngày " + DateTime.Now.Date);
                }
                else
                {
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Không thể Backup được!!","Thông báo!");
                return;
            }
        }

        private void buttonItem11_Click(object sender, EventArgs e)
        {
            try
            {
                flDialog = new OpenFileDialog();
                flDialog.Filter = "File type (*.bak)|*.bak|All file (*.*)|*.*";
                //flDialog.DefaultExt = ".bak";

                flDialog.ShowDialog();
                _Path = flDialog.FileName;
                if (_Path != "")
                {
                    backupAndRestore.RestoreDatabase(_Path);
                    MessageBox.Show("Restore thành công: ngày " + DateTime.Now.Date.Day + " - " + DateTime.Now.Date.Month);
                }
                else
                {
                    return;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi!"+ex.Message);
                return;
            }
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            ucNguoiDung ucNguoiDung = new ucNguoiDung();
            addNewTab("Tài khoản người dùng",ucNguoiDung);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (_Quyen == "NHÂN VIÊN")
            {
                rbHeThong.Enabled = false;
                //MessageBox.Show(_Quyen);
                ribbonTabItem2.Select();
            }
            else if(_Quyen=="ADMIN")
            {
                rbHeThong.Focus();
                rbHeThong.Select();
            }
            else
            {
                rbHeThong.Select();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //lblTime.Text += DateTime.Now.Hour + " : " + DateTime.Now.Minute + " : " + DateTime.Now.Second;
            string Gio, Phut, Giay;
            Gio = DateTime.Now.Hour.ToString() ;
            Phut = DateTime.Now.Minute.ToString();
            Giay = DateTime.Now.Second.ToString();
            int SoGiay = int.Parse(Giay);
            if (SoGiay%2==0)
            {
                //if (SoGiay < 9)
                //{
                //    Giay = "0" + Giay;
                //}
                lblTG.Text = Gio + " : " + Phut + "  " +Giay;
            }
            else
            {
                lblTG.Text = Gio + " : " + Phut + " : " +Giay;
            }
            lblNgay.Text = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
        }

        private void buttonItem34_Click(object sender, EventArgs e)
        {
            frmKVUT f = new frmKVUT();
            f.ShowDialog();
        }

        private void buttonItem12_Click(object sender, EventArgs e)
        {
            frmDanToc f = new frmDanToc();
            f.ShowDialog();
        }

        private void buttonItem36_Click(object sender, EventArgs e)
        {
            frmDTUT f = new frmDTUT();
            f.ShowDialog();
        }

        private void buttonItem33_Click(object sender, EventArgs e)
        {
            frmNgayThi f = new frmNgayThi();
            f.Show();
        }

        private void buttonItem16_Click(object sender, EventArgs e)
        {
            frmLogin f = new frmLogin();
            f.Show();
            Hide();
        }

        private void buttonItem20_Click(object sender, EventArgs e)
        {
            frmPhongThi f = new frmPhongThi();
            f.Show();
        }

        private void buttonItem18_Click(object sender, EventArgs e)
        {
            frmNganh f = new frmNganh();
            f.Show();
        }

        private void buttonItem19_Click(object sender, EventArgs e)
        {
            frmMonThi f = new frmMonThi();
            f.Show();
        }

        private void buttonItem37_Click(object sender, EventArgs e)
        {
            frmDotTuyenSinh f = new frmDotTuyenSinh();
            f.Show();
        }
    }
}
